﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CMSLibrary.TagsCreate.CustomFunction.Common
{
    /// <summary>
    /// 1.分隔符(行索引┇类型┇数值┇yes循环体┇no循环体)
    /// SplitChar(
    /// [1]  /#Index#/┇
    /// [2] {0.首尾
    ///      1.间隔(数值: 指定) }┇
    /// [3] 数值┇
    /// [4] ,┇
    /// [5] )
    /// 
    /// 2.分隔符(行索引)
    /// </summary>
    public class Custom_SplitChar : PFunctionBase
    {
        #region 成员
        protected int RowIndex = 1; //行索引
        protected const char SplitChar = '┇';  //分隔符
        protected string yes_char = string.Empty;   //是模板
        protected string no_char = ",";   //否模板
        protected int splittype = 0;    //分隔类型
        protected int splitvalue = 2;   //数值
        #endregion

        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = sourceParas.Split(SplitChar);

            //重载
            ///////////// 2
            try
            {
                RowIndex = Convert.ToInt32(paras[0]);
                splittype = Convert.ToInt32(paras[1]);
                splitvalue = Convert.ToInt32(paras[2]);
                yes_char = paras[3];
                no_char = paras[4];
                return;
            }
            catch { }
            //////////// 1
            try
            {
                RowIndex = Convert.ToInt32(paras[0]);
                return;
            }
            catch { throw new Exception("参数不正确"); }
        }

        public override string ToString()
        {
            string result = string.Empty;
            //验证数据
            if (splitvalue < 2) { splitvalue = 2; }

            switch (splittype)
            {
                case 1:
                    result = (RowIndex % splitvalue == 0) ? yes_char : no_char;
                    break;                
                default:
                    result = (RowIndex == 1) ? yes_char : no_char;
                    break;
            }
            return result;
        }
    }
}